package com.bsh.controller.partner;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bsh.dto.PartnerListQryDto;
import com.bsh.service.PartnerUpgradeAskService;
import com.bsh.utils.IpUtil;
import com.bsh.utils.PageJosnMessage;
import com.bsh.utils.PageTools;
import com.bsh.utils.Pager;
import com.mysql.jdbc.StringUtils;

@Controller
public class PartnerUpgradeAskController {
	private static Logger log = LoggerFactory.getLogger(PartnerUpgradeAskController.class);

	@Autowired
	HttpServletRequest request;
	@Autowired
	HttpServletResponse response;
	@Autowired
	PartnerUpgradeAskService askService;
	
	/**
	 * 合作伙伴申请列表
	 */
	@RequestMapping(value = "/partner/upgrade/ask/list")
	public ModelAndView partnerAskList(@ModelAttribute PartnerListQryDto dto) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/partner/upgradeAskList");

		String pageStr = request.getParameter("pageIndex");
		Integer pageIndex = Integer.parseInt(StringUtils.isNullOrEmpty(pageStr) ? "1" : pageStr);

		String sizeStr = request.getParameter("pageSize");
		Integer pageSize = Integer.parseInt(StringUtils.isNullOrEmpty(sizeStr) ? "10" : sizeStr);

		Pager pager = new Pager(pageIndex, pageSize);
		List<Map<String, Object>> list = askService.getAskPage(pager,dto);
		mv.addObject("list", list);
		mv.addObject("pager", pager);
		mv.addObject("qry", dto);
		return mv;
	}
	
	
	/**
	 * 审核通过
	 */
	@ResponseBody
	@RequestMapping(value = "/partner/upgrade/audit/pass")
	public PageJosnMessage pass(@RequestParam(value="askId") Integer askId,@RequestParam(value="partnerId") Integer partnerId) {
		String ip=IpUtil.getIpAddr(request);
        Integer res =  askService.audit(askId, partnerId,true,null,ip);
        PageJosnMessage msg=new PageJosnMessage();
        res=(res==null?1:res);
        msg.putData("status",res>0?1:res);
		return msg;
	}
	
	/**
	 * 审核未通过
	 */
	@ResponseBody
	@RequestMapping(value = "/partner/upgrade/audit/refuse")
	public ModelAndView refuse(@RequestParam(value="askId") Integer askId,@RequestParam(value="partnerId") Integer partnerId,@RequestParam(value="msg") String auditMsg) {
		String ip=IpUtil.getIpAddr(request);
        Integer res =  askService.audit(askId,partnerId, false,auditMsg,ip);
        ModelAndView mv=refuseMsg(askId,partnerId);
		mv.addObject("askId",askId);
		mv.addObject("partnerId",partnerId);
        mv.addObject("msg", auditMsg);
        
        String msg="";
        if(res==1)
        	msg = PageTools.alertCloseFraRefresh("审核合作伙伴申请","审核成功！",1000);
        else
        	msg = PageTools.alert("审核合作伙伴申请","审核失败，请稍后重试！",1000);
		mv.addObject("actionMsg",msg);
		return mv;
	}

	/**
	 * 添加审核消息
	 */
	@ResponseBody
	@RequestMapping(value = "/partner/upgrade/audit/msg/{askId}/{partnerId}")
	public ModelAndView refuseMsg(@PathVariable(value="askId") Integer askId,@PathVariable(value="partnerId") Integer partnerId) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/partner/auditAskMsg");
		mv.addObject("askId",askId);
		mv.addObject("partnerId",partnerId);
		return mv;
	}

}
